<!-- src/user_views/UserRegisterView.vue -->
<template>
  <div class="register-container">
    <div class="register-card">
      <div class="register-header">
        <div class="fish-icon">
          <i class="el-icon-fish"></i>
        </div>
        <h1>创建新账户</h1>
        <p>加入我们的钓鱼社区</p>
      </div>

      <el-form :model="registerForm" :rules="rules" ref="registerFormRef" class="register-form">
        <el-form-item prop="username">
          <el-input v-model="registerForm.username" placeholder="用户名 (5-16个字符)" prefix-icon="el-icon-user"
            size="large"></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input v-model="registerForm.password" type="password" placeholder="密码 (5-16个字符)"
            prefix-icon="el-icon-lock" show-password size="large"></el-input>
        </el-form-item>

        <el-form-item prop="confirmPassword">
          <el-input v-model="registerForm.confirmPassword" type="password" placeholder="确认密码" prefix-icon="el-icon-lock"
            show-password size="large"></el-input>
        </el-form-item>

        <el-form-item class="agreement-item">
          <el-checkbox v-model="agreement">
            我已阅读并同意 <a href="#" @click.prevent="showTerms">用户协议</a> 和 <a href="#" @click.prevent="showPrivacy">隐私政策</a>
          </el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="register-button" :loading="loading" @click="handleRegister" size="large">
            创建账户
          </el-button>
        </el-form-item>
      </el-form>

      <div class="login-link">
        已有账户？
        <a href="#" @click.prevent="handleLoginRedirect" v-if="!isRedirecting">立即登录</a>
        <span v-else class="redirect-loading">跳转中...</span>
      </div>
    </div>

    <div class="register-footer">
      <p>© 2024 钓鱼助手系统. 保留所有权利.</p>
    </div>
  </div>
</template>

<script>
import { ref, reactive, onBeforeUnmount } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { userAPI } from '@/api'

export default {
  name: 'UserRegisterView',
  setup() {
    const router = useRouter()
    const registerFormRef = ref()
    const loading = ref(false)
    const agreement = ref(false)
    const isRedirecting = ref(false)

     const handleLoginRedirect = () => {
      isRedirecting.value = true
      setTimeout(() => {
        router.push('/userLogin')
      }, 1000)
    }

    // 组件卸载前清理
    onBeforeUnmount(() => {
      loading.value = false
    })

    const registerForm = reactive({
      username: '',
      password: '',
      confirmPassword: ''
    })

    // 验证逻辑
    const rules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 5, max: 16, message: '用户名长度必须在 5 到 16 个字符之间', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 5, max: 16, message: '密码长度必须在 5 到 16 个字符之间', trigger: 'blur' }
      ],
      confirmPassword: [
        { required: true, message: '请确认密码', trigger: 'blur' },
        {
          validator: validateConfirmPassword,
          trigger: 'blur'
        }
      ]
    }

    // 自定义验证确认密码
    function validateConfirmPassword(rule, value, callback) {
      if (!value) {
        callback(new Error('请确认密码'))
      } else if (value != registerForm.password) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }

    // 显示用户协议
    const showTerms = () => {
      ElMessageBox.alert('请不要不在网站中搞七搞八，请您记住您的账户密码', '用户协议', {
        confirmButtonText: '确定'
      })
    }

    // 显示隐私政策
    const showPrivacy = () => {
      ElMessageBox.alert('请不要不把坏东西丢进我的网站', '隐私政策', {
        confirmButtonText: '确定'
      })
    }

    const handleRegister = async () => {
      if (!agreement.value) {
        ElMessage.error('请先同意用户协议和隐私政策')
        return
      }

      registerFormRef.value.validate(async (valid) => {
        if (valid) {
          loading.value = true

          try {
            // 使用统一API管理
            const response = await userAPI.register({
              username: registerForm.username,
              password: registerForm.password
            })

            // 处理成功响应
            if (response.code === 0) {
              ElMessage.success('注册成功')
              router.push('/userLogin')
            } else {
              ElMessage.error(response.message || '注册失败')
            }
          } catch (error) {
            // 处理错误响应
            ElMessage.error(error.message || '注册失败，请稍后重试')
          } finally {
            loading.value = false
          }
        }
      })
    }

    return {
      registerFormRef,
      registerForm,
      rules,
      loading,
      agreement,
      handleRegister,
      router,
      showTerms,
      showPrivacy,
      print,
      isRedirecting,
      handleLoginRedirect
    }
  }
}
</script>

<style scoped>
.register-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: rgb(202, 253, 255);
  padding: 20px;
  box-sizing: border-box;
}

.register-card {
  width: 100%;
  max-width: 450px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 20px;
  padding: 40px 30px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
  backdrop-filter: blur(10px);
  margin-bottom: 20px;
}

.register-header {
  text-align: center;
  margin-bottom: 30px;
}

.fish-icon {
  width: 80px;
  height: 80px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 20px;
}

.fish-icon i {
  font-size: 40px;
  color: white;
}

.register-header h1 {
  font-size: 28px;
  color: #333;
  margin: 0 0 10px;
  font-weight: 600;
}

.register-header p {
  color: #666;
  margin: 0;
  font-size: 16px;
}

.register-form {
  margin-top: 20px;
}

.register-form :deep(.el-form-item) {
  margin-bottom: 20px;
}

.register-form :deep(.el-input__wrapper) {
  border-radius: 12px;
  padding: 2px 15px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

.register-form :deep(.el-input__inner) {
  height: 48px;
  font-size: 16px;
}

.agreement-item {
  margin-top: 10px;
}

.agreement-item :deep(.el-checkbox__label) {
  font-size: 14px;
  color: #666;
  line-height: 1.5;
}

.agreement-item :deep(.el-checkbox__label a) {
  color: #667eea;
  text-decoration: none;
}

.agreement-item :deep(.el-checkbox__label a:hover) {
  text-decoration: underline;
}

.register-button {
  width: 100%;
  height: 50px;
  border-radius: 12px;
  font-size: 18px;
  font-weight: 600;
  margin-top: 10px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
}

.register-button:hover {
  opacity: 0.9;
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
}

.login-link {
  text-align: center;
  margin-top: 25px;
  color: #666;
  font-size: 15px;
}

.login-link a {
  color: #667eea;
  text-decoration: none;
  font-weight: 600;
  margin-left: 5px;
}

.login-link a:hover {
  text-decoration: underline;
}

.register-footer {
  color: rgba(255, 255, 255, 0.8);
  text-align: center;
  font-size: 14px;
}

.register-footer p {
  margin: 0;
}

/* 响应式设计 */
@media (max-width: 576px) {
  .register-card {
    padding: 30px 20px;
    margin: 0 15px 15px;
  }

  .register-header h1 {
    font-size: 24px;
  }

  .fish-icon {
    width: 70px;
    height: 70px;
  }

  .fish-icon i {
    font-size: 35px;
  }
}
</style>